package com.zrz.service;

import com.zrz.XmlUtils.CommonUtil;
import com.zrz.dao.auto.ImageMediaidMapper;
import com.zrz.dao.auto.UserMapper;
import com.zrz.domain.ImageUpload;
import com.zrz.model.auto.ImageMediaid;
import com.zrz.model.auto.ImageMediaidExample;
import com.zrz.model.auto.User;
import com.zrz.util.LogUtils;
import com.zrz.util.MenuManager;
import com.zrz.util.UploadImageUitl;
import com.zrz.util.WeiXinUtils;
import com.zrz.vo.Token;
import com.zrz.vo.WeixinUserInfo;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class WechatService {
    @Value("${appId:wxe0bd118fa5a91d7f}")
    private String appId;
    @Value("${secret:3e008193dc5d3fa61e2f7ce781b7b57b}")
    private String secret;
    @Value("${redirectUrl}")
    private String redirectUrl;
    @Value("${pageUrl}")
    private String pageUrl;
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private UserService userService;
    @Autowired
    private ImageMediaidMapper imageMediaidMapper;

    public void getSaveUserInfo(String openId, String params) {
        LogUtils.info("想骂街了"+openId);
        Token token = CommonUtil.getToken(appId, secret);
        if (token != null) {
            WeixinUserInfo weixinUserInfo = CommonUtil.getUserInfo(token.getAccessToken(), openId);
            if(StringUtils.isNotEmpty(weixinUserInfo.getUnionid())){
                User user= userService.getUserInfoByUnionId(weixinUserInfo.getUnionid());
                if(user!=null){
                    if(StringUtils.isEmpty(user.getOpenId())){
                        user.setOpenId(weixinUserInfo.getOpenId());
                        user.setSex(weixinUserInfo.getSex());
                        userService.updateByPrimaryKeySelective(user);
                    }
                    return ;
                }
            }
            if(StringUtils.isNotBlank(weixinUserInfo.getOpenId())){
               User user= userService.getUserInfoByOpenId(null,weixinUserInfo.getOpenId());
               if(user!=null){
                   return ;
               }
            }else{
                return ;
            }
            User user = new User();
            if(StringUtils.isEmpty(weixinUserInfo.getOpenId())){
                LogUtils.info("用户openid为空");
                return;
            }
            user.setOpenId(weixinUserInfo.getOpenId());
            user.setAvatar(weixinUserInfo.getHeadImgUrl());
            user.setCity(weixinUserInfo.getCity());
            user.setProvince(weixinUserInfo.getProvince());
            user.setSex(weixinUserInfo.getSex());
            user.setNickname(weixinUserInfo.getNickname());
            user.setUnionid(weixinUserInfo.getUnionid());
            user.setGender(weixinUserInfo.getSex());
            user.setRegisterTime(new Date());
            if (StringUtils.isNotEmpty(params)) {
                user.setParentId(Integer.parseInt(params));
            }
            userService.save(user);
            try {
                //生成图片
                String accessToken = WeiXinUtils.createForeverStrTicket(token.getAccessToken(), user.getId().toString());
                WeiXinUtils.showQrcode(accessToken, user.getId().toString() + ".jpg");
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

    }
    public int createMenu(){
        return MenuManager.addMenu(appId,secret,pageUrl);
    }

    public List getMediaid(){
        ImageMediaidExample imageMediaidExample=new ImageMediaidExample();
        imageMediaidExample.createCriteria();
        List<ImageMediaid> list=imageMediaidMapper.selectByExample(imageMediaidExample);
        list.forEach(l->{
            ImageUpload imageUpload=UploadImageUitl.uploadTempMedia(appId,secret,l.getName());
            l.setMediaid(imageUpload.getMedia_id());
            l.setUpdateTime(new Date());
            imageMediaidMapper.updateByPrimaryKey(l);

        });
        return list;
    }

    public ImageMediaid getMedidByName(String name){
        ImageMediaidExample imageMediaidExample=new ImageMediaidExample();
        imageMediaidExample.createCriteria().andNameEqualTo(name);
        List<ImageMediaid> list=imageMediaidMapper.selectByExample(imageMediaidExample);
        if(list.size()>0){
            return list.get(0);
        }else{
            return  null;
        }
    }
}
